package com.wwz.leetcode;

import com.wwz.common.TreeNode;
import org.junit.Test;

/**
 * @description: 二叉树的最大深度
 * @author: Sailor
 * @date: 2023-12-17 13:44:35
 */
public class LeetCode104 {

    @Test
    public void test(){
        System.out.println(maxDepth2(new TreeNode(3, new TreeNode(9, null, null), new TreeNode(20, new TreeNode(15, null, null), new TreeNode(7, null, null)))));
    }


    public int maxDepth(TreeNode root) {
        return root == null ? 0 : Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
    }

    public int maxDepth2(TreeNode root) {
        if (root == null) {
            return 0;
        } else {
            int leftHeight = maxDepth2(root.left);
            int rightHeight = maxDepth2(root.right);
            return Math.max(leftHeight, rightHeight) + 1;
        }
    }

}
